<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectSubType">
              <a-select v-model="model.projectSubType" placeholder="请选择项目类型" :options="projectSubTypeOptions" @change="projectSubTypeChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="时间节点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timeNodeCode">
              <a-select v-model="model.timeNodeCode" :options="timeNodeOptions" placeholder="请选择开始时间节点"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="比例区间开始（%）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="presentStart">
              <a-input-number v-model="model.presentStart" placeholder="请输入整数" style="width: 100%" :min="0" :max="100" :formatter="value => `${value}%`"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="比例区间结束（%）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="presentEnd">
              <a-input-number v-model="model.presentEnd" placeholder="请输入整数" style="width: 100%" :min="0" :max="100" :formatter="value => `${value}%`"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="节点累计提取比例上限（%）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cumulativePresentLimit">
              <a-input-number v-model="model.cumulativePresentLimit" placeholder="请输入整数" style="width: 100%" :min="0" :max="100"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="成果目标权重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="targetWeight">
              <a-input-number v-model="model.targetWeight" placeholder="请输入小数，与进度目标权重相加等于1" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="进度目标权重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="progressWeight">
              <a-input-number v-model="model.progressWeight" placeholder="请输入小数，与成果目标权重相加等于1" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="经济目标达成率占比（%）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="progressWeight">
              <a-input-number v-model="model.economyTargetWeight" placeholder="请输入数字" style="width: 100%" :min="0" :max="100" :formatter="value => `${value}%`"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="需要重跑价值表" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="needRerunTest">
              <a-select v-model="model.needRerunTest" :options="needRerunTestOptions" placeholder="请选择是否需要重跑价值表"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="memo">
              <a-textarea v-model="model.memo" placeholder="请输入说明,200字内"  :auto-size="{ minRows: 1, maxRows: 100 }" :maxLength="200" ></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ProjectCycleRulesForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      projectSubTypeOptions: {
        Type: Array,
        default: () => [],
      }
    },
    data () {
      return {
        timeNodeList:[],
        timeNodeOptions:[],
        needRerunTestOptions:[{value:true,label:'是'},{value:false,label:'否'}],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          projectSubType: [
              { required: true, message: '请选择项目类型'},
           ],
          timeNodeCode: [
              { required: true, message: '请选择时间节点'},
           ],
          presentStart: [
              { required: true, message: '请输入比例区间开始值'},
           ],
          presentEnd: [
              { required: true, message: '请输入比例区间结束值'},
           ],
          needRerunTest: [
              { required: true, message: '请选择是否需要重跑价值表'},
           ],
          targetWeight: [
              { required: true, message: '成果目标权重必填'},
           ],
          progressWeight: [
              { required: true, message: '进度目标权重必填'},
           ],
        },
        url: {
          add: "/projectRewardRules/add",
          edit: "/projectRewardRules/edit",
          queryById: "/projectRewardRules/queryById",
          getCodeInfoList: "/manage/codeInfo/queryByType",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        this.getTimeNodeList(this.model.projectSubType)
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.$emit('checkRepeat',this.model);
            if(this.model.isRepeat){
              return;
            }
            let targetWeight=parseFloat(this.model.targetWeight);
            let progressWeight=parseFloat(this.model.progressWeight);
            if(isNaN(targetWeight)||isNaN(progressWeight)||parseFloat((targetWeight+progressWeight).toFixed(2))!==1){
              this.$message.warning('权重值不正确或者相加不为1');
              return;
            }
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      projectSubTypeChange(value){
        this.getTimeNodeList(value)
      },
      getTimeNodeList(projectSubType){
        const that=this;
        getAction(this.url.getCodeInfoList,{type:projectSubType+"类项目时间节点"}).then(res=>{
          this.timeNodeList=res.result||[];
          if(this.timeNodeList.length>0){
            this.timeNodeList.forEach(function(item){
              that.timeNodeOptions.push({value:item.code.trim(),label:item.name.trim()});
            })
          }else{
            that.timeNodeOptions=[];
          }
        })
      },
    }
  }
</script>